package questionLibrary;

import java.sql.SQLException;

public class QuestionInfo {

	private String question;

	private int questionNumber;

	private String[] answer;
	
	SqlWorkerInterfase worker;
	
	private OrganInfo[] organEffect;

	/**
	 * This is constructor, that sets question and answers.
	 * 
	 * @param worker
	 * 
	 * @param question
	 *            is question that will copy to inner string.
	 * @param answers
	 *            is array of strings, each of which is possible answer.
	 */
	public QuestionInfo(SqlWorkerInterfase worker, String question,
			int questionNumber, String... answers) {

		this.question = new String(question);
		this.questionNumber = questionNumber;
		this.worker=worker;
		answer = answers.clone();
		
		organEffectSetter();

	}

	private void organEffectSetter() {
		organEffect = new OrganInfo[answer.length];
		for (int i = 0; i < organEffect.length; i++)
			organEffect[i] = new OrganInfo();
	}

	/**
	 * This method is used for getting text of question.
	 * 
	 * @return text of question.
	 */
	public String getQuestion() {
		return new String(question);
	}

	/**
	 * This method is used for getting text of answers.
	 * 
	 * @return text of answers.
	 */
	public String[] getAnswers() {
		return answer.clone();
	}

	/**
	 * This method returns effect for some parameter param for organ organ.
	 * 
	 * @param organ
	 *            is organ, which will get effects of answers.
	 * @param param
	 *            is parameter of organ, which will be changed (for example,
	 *            energy of brain).
	 * @param answer_number
	 *            is number of answers which affects on some effect of some
	 *            organ.
	 * @return summary effect of all answers for some param of some organ.
	 */
	public int getEffect(int organ, int param, int... answerNumber) {
		int result = 0;
		try {
			for (int numberOfAnswer :answerNumber) {
				result += worker.currentEffect(organ, param, questionNumber,
						numberOfAnswer);
			}			
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return result;
	}

	
	public int getCountOfAnswers() {
		return answer.length;
	}

	/**
	 * This method is used for getting entire text (with question and answers)
	 * to show it on the screen.
	 * 
	 * @return string which may be shown on the screen.
	 */
	public String toString() {
		StringBuilder questionWithAnswers = new StringBuilder();
		questionWithAnswers.append(question);
		questionWithAnswers.append("\nPossible answers:\n");
		for (int numberOfAnswer = 0; numberOfAnswer < answer.length; numberOfAnswer++) {
			questionWithAnswers.append(numberOfAnswer);
			questionWithAnswers.append(". ");
			questionWithAnswers.append(answer[numberOfAnswer]);
			questionWithAnswers.append("\n");
		}
		return questionWithAnswers.toString();
	}

}